package com.hyt.it.ogt.kq.service.gov.arrange;

import com.hyt.core.base.BaseEntity;
import com.hyt.it.ogt.kq.common.enums.ArrangeEnum;
import com.hyt.it.ogt.kq.service.gov.model.dto.ProgressResult;
import com.hyt.it.ogt.kq.service.gov.model.entity.Task;
import com.hyt.it.ogt.kq.service.gov.model.entity.TimeRoomParam;
import com.hyt.it.ogt.kq.service.gov.model.param.ArrangeParam;
import com.hyt.it.ogt.kq.service.gov.service.ITaskService;
import com.hyt.it.ogt.kq.service.gov.service.ITimeRoomParamService;
import com.hyt.it.ogt.kq.service.gov.service.TimeRoomExaminerCacheService;
import com.hyt.progress.service.ProgressManager;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 编排
 */
@Component
public class ArrangeFactory {
    @Resource
    private ImportArrangeAdapter importArrangeAdapter;
    @Resource
    private RoomArrangeAdapter roomArrangeAdapter;
    @Resource
    private VirtualRoomArrangeAdapter virtualRoomArrangeAdapter;
    @Resource
    private TimeRoomExaminerCacheService timeRoomExaminerCacheService;
    @Resource
    private ITaskService iTaskService;
    @Resource
    private ITimeRoomParamService iTimeRoomParamService;

    public ArrangeFactory() {}

    /**
     *
     * @param param
     * @param arrangeEnum
     */
    @SneakyThrows
    @Transactional(rollbackFor = Exception.class)
    public void arrange(ArrangeParam param, ArrangeEnum arrangeEnum) {
        ProgressManager.updateProgress(3L);
        ArrangeAbstractService arrangeAbstractService = this.getService(arrangeEnum);
        Object result = arrangeAbstractService.arrange(param);
        iTaskService.lambdaUpdate().set(Task::getArrangeDate, LocalDateTime.now()).eq(BaseEntity::getId, param.getTaskId()).update();
        // 清理监控老师相关的缓存
        timeRoomExaminerCacheService.clearUserProctorCache(null, param.getTaskId());

        if(!(arrangeAbstractService instanceof ImportArrangeAdapter)) {
            List<TimeRoomParam> timeRoomParamList = new ArrayList<>();
            param.getTimeRoomParamVOList().stream().forEach(item -> {
                TimeRoomParam build = TimeRoomParam.builder().value(item.getValue()).build();
                build.setId(item.getId());
                timeRoomParamList.add(build);
            });
            iTimeRoomParamService.updateBatchById(timeRoomParamList);
        }

        if(result == null) {
            ProgressManager.finish(ProgressResult.builder().success(Boolean.TRUE).build());
        } else {
            ProgressManager.finish(ProgressResult.builder().success(Boolean.FALSE).result(result).build());
        }
    }

    public ArrangeAbstractService getService(ArrangeEnum arrangeEnum) {
        ArrangeAbstractService adapter = null;
        switch (arrangeEnum) {
            case ARRANGE_BY_IMPORT:
                adapter = importArrangeAdapter;
                break;
            case ARRANGE_BY_VIRTUAL_ROOM:
                adapter = virtualRoomArrangeAdapter;
                break;
            case ARRANGE_BY_ROOM:
                adapter = roomArrangeAdapter;
                break;
            default:
                break;
        }
        return adapter;
    }
}
